<!DOCTYPE HTML> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Acceleration 例子</title> 
<script type="text/javascript" src="cordova.js"></script> 
<link rel="stylesheet" type="text/css" href="css/index.css">
<script type="text/javascript">
var watchID = null;  
document.addEventListener("deviceready", onDeviceReady, false);  
 var oldValue = { 
     x: null, 
     y: null, 
     z: null 
 }  
    function onDeviceReady() { 
       startWatch(); 
    }   
    function startWatch() { 
       var options = { frequency: 300 };  
       watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options); 
    }  
    function stopWatch() { 
        if (watchID) { 
           navigator.accelerometer.clearWatch(watchID); 
           watchID = null; 
        } 
    }  
    // 获取加速度信息成功后的回调函数 
    function onSuccess(newValue) { 
        var changes = {}, 
        bound = 3; 
        if (oldValue.x !== null) { 
           changes.x = Math.abs(oldValue.x, newValue.x); 
           changes.y = Math.abs(oldValue.y, newValue.y); 
           changes.z = Math.abs(oldValue.z, newValue.z); 
        } 
        if (changes.x> bound &&changes.y> bound &&changes.z> bound) {   
            alert('检测到手机晃动'); 
        } 
        oldValue = { 
           x: newValue.x, 
           y: newValue.y, 
           z: newValue.z 
        } 
        var element = document.getElementById('accelerometer'); 
        element.innerHTML = 'Acceleration X: ' + newValue.x + '<br />' + 
                            'Acceleration Y: ' + newValue.y + '<br />' + 
                            'Acceleration Z: ' + newValue.z + '<br />' +  
                            'Timestamp: '      + newValue.timestamp + '<br />'; 
    }  
    // 获取加速度信息失败后的回调函数 
    function onError() { 
       alert('onError!'); 
    }  
</script> 
</head> 
<body> 
<h1>Shake Page</h1>
<div id="accelerometer">监测加速度信息中...</div> 
<button onclick="stopWatch();">停止监测加速度信息</button> 
</body> 
</html>